package utils

import (
	"fyne.io/fyne/v2"
	"regexp"
	"strconv"
	"strings"
)

func ParsePosition(s string) fyne.Position {
	parts := strings.Fields(s) // 按空白字符分割
	if len(parts) != 2 {
		return fyne.NewPos(0, 0)
	}

	x, err := strconv.ParseFloat(parts[0], 64)
	if err != nil {
		return fyne.NewPos(0, 0)
	}

	y, err := strconv.ParseFloat(parts[1], 64)
	if err != nil {
		return fyne.NewPos(0, 0)
	}

	return fyne.NewPos(float32(x), float32(y))
}

func ParseSize(s string) fyne.Size {
	parts := strings.Fields(s) // 按空白字符分割
	if len(parts) != 2 {
		return fyne.NewSize(0, 0)
	}

	x, err := strconv.ParseFloat(parts[0], 64)
	if err != nil {
		return fyne.NewSize(0, 0)
	}

	y, err := strconv.ParseFloat(parts[1], 64)
	if err != nil {
		return fyne.NewSize(0, 0)
	}

	return fyne.NewSize(float32(x), float32(y))
}

func Max(a, b float32) float32 {
	if a > b {
		return a
	}
	return b
}

func FormatXmlString(formattedXML string) string {
	reg := regexp.MustCompile(`<Property\s+([^>]*?)>(.*?)</Property>`)
	formattedXML = reg.ReplaceAllString(formattedXML, `<Property $1 />`)
	reg2 := regexp.MustCompile(`<LoadSkin\s+([^>]*?)>(.*?)</LoadSkin>`)
	return reg2.ReplaceAllString(formattedXML, `<LoadSkin $1 />`)
}
